package com.sh.data.engine.job.admin.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sh.data.engine.job.admin.core.model.XxlJobRegistry;
import org.apache.ibatis.annotations.Mapper;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by xuxueli on 16/9/30.
 */
@Mapper
public interface XxlJobRegistryDao extends BaseMapper<XxlJobRegistry> {

    default List<Integer> findDead(int timeout, Date nowTime) {
        LambdaQueryWrapper<XxlJobRegistry> wrapper = new QueryWrapper<XxlJobRegistry>().lambda();
        wrapper.lt(XxlJobRegistry::getUpdateTime, new Date(nowTime.getTime() - timeout * 1000));
        List<XxlJobRegistry> xxlJobRegistries = this.selectList(wrapper);
        return xxlJobRegistries.stream().map(XxlJobRegistry::getId).collect(Collectors.toList());
    }

    default int removeDead(List<Integer> ids) {
        return this.delete(new QueryWrapper<XxlJobRegistry>().lambda().in(XxlJobRegistry::getId, ids));
    }

    default List<XxlJobRegistry> findAll(int timeout, Date nowTime) {
        LambdaQueryWrapper<XxlJobRegistry> wrapper = new QueryWrapper<XxlJobRegistry>().lambda();
        wrapper.gt(XxlJobRegistry::getUpdateTime, new Date(nowTime.getTime() - timeout * 1000));
        List<XxlJobRegistry> xxlJobRegistries = this.selectList(wrapper);
        return xxlJobRegistries;
    }

    default int registryUpdate(
        String registryGroup, String registryKey, String registryValue, Date updateTime) {
        LambdaUpdateWrapper<XxlJobRegistry> update = new UpdateWrapper<XxlJobRegistry>().lambda();
        update
            .eq(XxlJobRegistry::getRegistryKey, registryGroup)
            .eq(XxlJobRegistry::getRegistryKey, registryKey)
            .eq(XxlJobRegistry::getRegistryValue, registryValue)
            .set(XxlJobRegistry::getUpdateTime, updateTime);
        return this.update(null, update);
    }

    default int registrySave(
        String registryGroup, String registryKey, String registryValue, Date updateTime) {
        XxlJobRegistry xxlJobRegistry = new XxlJobRegistry();
        xxlJobRegistry.setRegistryGroup(registryGroup);
        xxlJobRegistry.setRegistryKey(registryKey);
        xxlJobRegistry.setRegistryValue(registryValue);
        xxlJobRegistry.setUpdateTime(updateTime);
        return this.insert(xxlJobRegistry);
    }

    default int registryDelete(String registryGroup, String registryKey, String registryValue) {
        return this.delete(
            new QueryWrapper<XxlJobRegistry>()
                .lambda()
                .eq(XxlJobRegistry::getRegistryGroup, registryGroup)
                .eq(XxlJobRegistry::getRegistryKey, registryKey)
                .eq(XxlJobRegistry::getRegistryValue, registryValue));
    }
}
